The TADS Alternate Library
Version 2.0

Object Reactions


Copyright 2000 by Kevin Forchione.
This is part of the TADS Alternate Library Authors Manual.

Introduction and Table of Contents




Object Reactions

 

Alt implements object reactions for the preAction, postAction, and endCommand stages of command execution. This allows an author to adopt a more object-oriented approach, keeping coding that affects an object’s behaviours within the object definition. It also allows an author to override certain default library actions.

 

A rough sequence of execution shows where object reactions fit into the scheme of command execution.

 

·         preCommand()

·         verbAction()

o        actorPreAction()

o        locPreAction

o        scopePreAction()

o        iobjPreAction()

o        dobjPreAction()

·         actorAction

·         roomAction

·         xobjCheck() / xobjGen()

·         verXoVerb() / xoVerb() / action()

·         postAction()

o        actorPostAction()

o        locPostAction()

o        scopePostAction()

o        iobjPostAction()

o        dobjPostAction()

·         fuses / daemons / notifies

·         endCommand()

o        actorEndCommand()

o        locEndCommand()

o        scopeEndCommand()

o        iobjEndCommand()

o        dobjEndCommand()

 

 

The preAction and postAction Methods

 

If an object’s xxxPreAction() or xxxPostAction() method returns true then the preAction or postAction process terminates and control passes to daemon and endCommand processing.

 

You can use preAction and postAction methods to override the behaviour of the default library as well. For example, suppose you have an object that you wish to have display a special message when taken, instead of the library default message “Taken.”. Normally, in TADS you would have to override the doTake() method, copying most of the original method code into your override method simply to facilitate the one-line change.

 

But using object reactions you can accomplish the change easily with the dobjPostAction method. For example, the following is the berry you can eat.

 

redBerry: FoodItem

    location = forestClearing

    sDesc = "red berry"

    noun = 'berry'

    adjective = 'red'

    bulk = 1

    initial = "A red berry clings tenaciously to a bush by a slender

stem ."

    lDesc = "The berry’s lustre makes your mouth water, but you seem

to recall that some berries are poisonous. "

    dobjPostAction = {

        switch(gVerb()) {

            case takeVerb:

                if (self.hasMovedCount == 1)

                    "You pick the berry, neatly cleaving its slender stem. ";

                else

                    "You pick up the now bruised berry.";

                return true;

            case dropVerb:

                "The berry drops to the ground, battered slightly. ";

                return true;

        }

    }

;

 

The postAction reactions only occur if the action methods have returned EC_SUCCESS. Because Alt captures the messages from the action stage any successful action message will be replaced by the postAction message if the postAction method returns true. In this instance the normal “Taken.” message is replaced with one of two messages from postAction.